#include "process.h"

#define SIZE 101
#define STYLE '#'

void FlushProcess(const char* tips, double total, double cur)
{
    const char* laber = "-\\|/";
    static int index = 0;
    int len = strlen(laber);

    char buffer[SIZE] = {0};
    double rate = cur*100.0/total;
    int num = (int)rate;

    int i = 0;
    for(i = 0; i < num; i++)
        buffer[i] = STYLE;

    printf("%s[%-100s][%.1lf%%][%c]\r", tips, buffer, rate, laber[index++]);
    index %= len;
    fflush(stdout);
}

void process()
{
    int rate = 0;
    char buffer[SIZE] = {0};
    const char* laber = "-\\|/";
    int len = strlen(laber);
    while(rate <= 100)
    {
        printf("[%-100s][%d%%][%c]\r", buffer, rate, laber[rate % len]);
        fflush(stdout);
        buffer[rate] = STYLE;
        rate++;
        usleep(50000);
    }
    printf("\n");
}
